<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
namespace Admin\Controller;
use Think\AjaxPage;
use Think\Page;
use Admin\Model\UserModel;
class PromotionController extends BaseController {
    //优惠券列表
    function coupon(){
        $mod = M('goods_coupon');
        $where = ' 1 = 1 ';
        $count = $mod->where($where)->count();
        $Page = new AjaxPage($count, 10);
        $show = $Page->show();
        $order_str = "add_time desc";
        $couponList = $mod->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $coupon_mod = M('users_coupon');
        foreach((array)$couponList as $k=>$v){
            $couponList[$k]['user_issued'] = $coupon_mod->where('order_id>0')->count();
        }
        $this->assign('couponList', $couponList);
        $this->assign('page', $show);
        $this->display();
    }
    function c_detail(){
        $mod = M('goods_coupon');
        if(IS_POST){
            $data = I('post.');
            if(!$data['title']){
                $this->error('请输入优惠券标题');
            }
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            if($data['act']=='add'){
                $data['add_time'] = time();
                $result = $mod->add($data);
                if($result){
                    if($data['c_type']==2){
                        $c_mod = M('users_coupon');
                        $w['status'] = 1;
                        if($data['modus']==1){
                            $user_list = M('users')->where($w)->order('add_time asc')->select();
                        }else{
                            $user_list = M('users')->where($w)->field('id,(select sum(order_price) from '.C('DB_PREFIX').'orders where 1=1) as price')->order('price asc')->select();
                        }
                        $c_data['cou_id'] = $result;
                        $c_data['add_time'] = time();
                        foreach((array)$user_list as $k=>$v){
                            $issued = $mod->where(array('id'=>$result))->getField('issued');
                            if($issued<$data['coupon_number']) {
                                $c_data['user_id'] = $v['id'];
                                $c_mod->add($c_data);
                                $mod->where(array('id' =>$result))->setInc('issued', 1);
                            }
                        }
                    }
                    $this->success('添加成功',U('Admin/Promotion/coupon'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $data['up_time'] = time();
                $result = $mod->where(array('id'=>$data['id']))->save($data);
                if($result){
                    if($data['c_type']==2){
                        $c_mod = M('users_coupon');
                        $user_id = $c_mod->where(array('cou_id'=>$data['id']))->getField('user_id',true);
                        $w['status'] = 1;
                        $user_id[0] && $w['id'] = array('not in',implode(',',$user_id));
                        if($data['modus']==1){
                            $user_list = M('users')->where($w)->order('add_time asc')->select();
                        }else{
                            $user_list = M('users')->where($w)->field('id,(select sum(order_price) from '.C('DB_PREFIX').'orders where 1=1) as price')->order('price asc')->select();
                        }
                        $c_data['cou_id'] = $data['id'];
                        $c_data['add_time'] = time();
                        foreach((array)$user_list as $k=>$v){
                            $issued = $mod->where(array('id'=>$data['id']))->getField('issued');
                            if($issued<$data['coupon_number']) {
                                $c_data['user_id'] = $v['id'];
                                $c_mod->add($c_data);
                                $mod->where(array('id' => $data['id']))->setInc('issued', 1);
                            }
                        }
                    }
                    $this->success('编辑成功',U('Admin/Promotion/coupon'));
                }else{
                    $this->error('编辑失败');
                }
            }
        }else{
            $id = I('id')?I('id'):'';
            if($id){
                $info = $mod->where(array('id'=>$id))->find();
                $this->assign('info',$info);
                $act = 'edit';
            }else{
                $act = 'add';
            }
            $this->assign('act',$act);
            $this->display();
        }
    }
    //满减列表
    function full_down(){
        $mod = M('goods_promotion');
        $where = ' type_id = 1 ';
        $count = $mod->where($where)->count();
        $Page = new AjaxPage($count, 10);
        $show = $Page->show();
        $order_str = "add_time desc";
        $goodsList = $mod->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('goodsList', $goodsList);
        $this->assign('page', $show);
        $this->display();
    }
    function f_detail(){
        $mod = M('goods_promotion');
        if(IS_POST){
            $data = I('post.');
            if(!$data['title']){
                $this->error('请输入活动名称');
            }
            $data['goods_list'] = rtrim($data['goods_list'],',');
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            if($data['act']=='add'){
                $data['add_time'] = time();
                $data['add_ip'] = getIP();
                $result = $mod->add($data);
                if($result){
                    $this->success('添加成功',U('Admin/Promotion/full_down'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $data['up_time'] = time();
                $result = $mod->where(array('id'=>$data['id']))->save($data);
                if($result){
                    $this->success('编辑成功',U('Admin/Promotion/full_down'));
                }else{
                    $this->error('编辑失败');
                }
            }
        }else{
            $id = I('id')?I('id'):'';
            $where = 'status=1';
            $time = time();
            if($id){
                $promotion = $mod->where(array('id'=>$id))->find();
                if($promotion['end_time']<$time){
                    $this->error('已结束的活动不能再次编辑');
                }else if($promotion['end_time']>$time && $promotion['start_time']<$time){
                    $this->error('活动期间不能再次编辑');
                }
                $where.=' and id not in('.$promotion['goods_list'].')';
                $this->assign('info',$promotion);
                $right_list = M('goods')->where('id in('.$promotion['goods_list'].')')->select();
                $this->assign('right_list',$right_list);
                $act = 'edit';
            }else{
                $act = 'add';
            }
            $goods_id = $mod->where('status=1')->getField('goods_list',true);
            if(count($goods_id)){
                $goods_id = implode(',',$goods_id);
                $where.=' and id not in('.$goods_id.')';
            }
            $this->assign('act',$act);
            $goods_list = M('goods')->where($where)->select();
            $this->assign('goods_list',$goods_list);
            $this->display();
        }
    }
    //限购列表
    function purchase(){
        $mod = M('goods_promotion');
        $where = ' a.type_id = 3 ';
        $count = $mod->alias('as a')->where($where)->count();
        $Page = new AjaxPage($count, 10);
        $show = $Page->show();
        $order_str = "a.add_time desc";
        $goodsList = $mod->alias('as a')
            ->join(C('DB_PREFIX').'goods as b on a.goods_list=b.id')
            ->join(C('DB_PREFIX').'goods_store_spec as c on c.id=a.spec_id')
            ->join(C('DB_PREFIX').'business as d on d.id=a.store_id')
            ->field('a.*,b.goods_name,c.spec_name,d.store_name')
            ->where($where)
            ->order($order_str)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('goodsList', $goodsList);
        $this->assign('page', $show);
        $this->display();
    }
    function p_detail(){
        $mod = M('goods_promotion');
        if(IS_POST){
            $data = I('post.');
            if(!$data['title']){
                $this->error('请输入活动名称');
            }
            if(!$data['store_id']){
                $this->error('请选择商家');
            }
            if(!$data['goods_list']){
                $this->error('请选择商品');
            }
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $price = M('goods_store_spec')->where(array('id'=>$data['spec_id']))->getField('spec_price');
            if($data['use_price']>$price){
                $this->error('限购金额不能大于商品售价');
            }
            if($data['act']=='add'){
                $data['add_time'] = time();
                $data['add_ip'] = getIP();
                $result = $mod->add($data);
                if($result){
                    $this->success('添加成功',U('Admin/Promotion/purchase'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $data['up_time'] = time();
                $result = $mod->where(array('id'=>$data['id']))->save($data);
                if($result){
                    $this->success('编辑成功',U('Admin/Promotion/purchase'));
                }else{
                    $this->error('编辑失败');
                }
            }
        }else{
            $id = I('id')?I('id'):'';
            if($id){
                $promotion = $mod->where(array('id'=>$id))->find();
                $promotion['shop_price'] = M('goods')->where(array('id'=>$promotion['goods_list']))->getField('shop_price');
                $this->assign('info',$promotion);
                $act = 'edit';
            }else{
                $act = 'add';
            }
            $this->assign('act',$act);
            $business_list = M('business')->where(array('status'=>1))->select();
            $this->assign('business_list',$business_list);
            $this->display();
        }
    }
    function ajaxGoods(){
        $id = I('store_id')?I('store_id'):'';
        if($id){
            $mod = M('goods_promotion');
            $time = time();
            $where['a.status'] = 1;
            $where['a.store_id'] = $id;
            $spec_id = $mod->where(array('start_time'=>array('lt',$time),'end_time'=>array('gt',$time)))->getField('spec_id',true);
            if(count($spec_id)){
                $where['a.spec_id'] = array('not in',implode(',',$spec_id));
            }
            $goods_id = $mod->where(array('start_time'=>array('lt',$time),'end_time'=>array('gt',$time)))->getField('goods_list',true);
            if(count($goods_id)){
                $where['b.id'] = array('not in',implode(',',$goods_id));
            }
            $goods_list = M('goods_store_spec')
                ->alias('as a')->join(C('DB_PREFIX').'goods as b on a.goods_id=b.id')
                ->field('b.goods_name,a.goods_id,a.spec_price,a.spec_name,a.store_id,a.id as spec_id')
                ->where($where)
                ->select();
            exit(json_encode(array('code'=>200,'result'=>$goods_list)));
        }
    }

    function special(){
        $mod = M('goods_promotion');
        $where = ' a.type_id = 2 ';
        $count = $mod->alias('as a')->where($where)->count();
        $Page = new AjaxPage($count, 10);
        $show = $Page->show();
        $order_str = "a.add_time desc";
        $goodsList = $mod->alias('as a')->join(C('DB_PREFIX').'goods as b on a.goods_list=b.id')->field('a.*,b.goods_name')->where($where)->order($order_str)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('goodsList', $goodsList);
        $this->assign('page', $show);
        $this->display();
    }
    function s_detail(){
        $mod = M('goods_promotion');
        if(IS_POST){
            $data = I('post.');
            if(!$data['title']){
                $this->error('请输入活动名称');
            }
            $data['start_time'] = strtotime($data['start_time']);
            $data['end_time'] = strtotime($data['end_time']);
            $price = M('goods')->where(array('id'=>$data['goods_list']))->getField('shop_price');
            if($data['use_price']>$price){
                $this->error('限购金额不能大于商品售价');
            }
            if($data['act']=='add'){
                $data['add_time'] = time();
                $data['add_ip'] = getIP();
                $result = $mod->add($data);
                if($result){
                    $this->success('添加成功',U('Admin/Promotion/special'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $data['up_time'] = time();
                $result = $mod->where(array('id'=>$data['id']))->save($data);
                if($result){
                    $this->success('编辑成功',U('Admin/Promotion/special'));
                }else{
                    $this->error('编辑失败');
                }
            }
        }else{
            $id = I('id')?I('id'):'';
            $w = '1=1';
            $where = 'status=1';
            $time = time();
            if($id){
                $promotion = $mod->where(array('id'=>$id))->find();
                if($promotion['end_time']<$time){
                    $this->error('已结束的活动不能再次编辑');
                }else if($promotion['end_time']>$time && $promotion['start_time']<$time){
                    $this->error('活动期间不能再次编辑');
                }
                $promotion['shop_price'] = M('goods')->where(array('id'=>$promotion['goods_list']))->getField('shop_price');
                $this->assign('info',$promotion);
                $w.=' and id!='.$id;
                $act = 'edit';
            }else{
                $act = 'add';
            }
            $goods_id = $mod->where($w)->getField('goods_list',true);
            if(count($goods_id)){
                $goods_id = implode(',',$goods_id);
                $where.=' and id not in('.$goods_id.')';
            }
            $this->assign('act',$act);
            $goods_list = M('goods')->where($where)->select();
            $this->assign('goods_list',$goods_list);
            $this->display();
        }
    }
    function e_detail(){
        $id = I('id')?I('id'):'';
        $mod = M('goods_promotion');
        $promotion = $mod->where(array('id'=>$id))->find();
        $promotion['goods'] = M('goods')->where('id in('.$promotion['goods_list'].')')->field('goods_name')->select();
        $promotion['shop_price'] = M('goods')->where(array('id'=>$promotion['goods_list']))->getField('shop_price');
        $this->assign('info',$promotion);
        $this->display();
    }
}
